<script setup lang="ts">
import { ref } from "vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { usePermissionPage } from "./utils/hook";

import Plus from "~icons/ep/plus";
import EditPen from "~icons/ep/edit";
import Delete from "~icons/ep/delete";

defineOptions({ name: "UserPermissions" });

const tableRef = ref();

const {
  filterForm,
  clientTypes,
  statusOptions,
  pagination,
  tableData,
  columns,
  loading,
  handleSearch,
  handleResetFilters,
  handlePageChange,
  handleSizeChange,
  permissionDialogVisible,
  permissionDialogTitle,
  permissionForm,
  permissionFormRef,
  rules,
  saving,
  openCreate,
  openEdit,
  submitPermission,
  handleDelete,
  permissionTypes
} = usePermissionPage();
</script>

<template>
  <div class="permissions-page">
    <el-form
      :inline="true"
      :model="filterForm"
      class="search-form bg-bg_color w-full pl-4 pt-3 pb-2 overflow-auto"
    >
      <el-form-item label="关键词：" prop="keyword">
        <el-input
          v-model="filterForm.keyword"
          placeholder="权限名称 / 标识"
          clearable
          class="w-[220px]!"
        />
      </el-form-item>
      <el-form-item label="所属端：" prop="client_type">
        <el-select
          v-model="filterForm.client_type"
          placeholder="全部端"
          clearable
          class="w-[180px]!"
        >
          <el-option label="全部端" value="" />
          <el-option
            v-for="item in clientTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态：" prop="status">
        <el-select
          v-model="filterForm.status"
          placeholder="全部状态"
          clearable
          class="w-[160px]!"
        >
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading"
          @click="handleSearch"
        >
          搜索
        </el-button>
        <el-button
          :icon="useRenderIcon('ep:refresh')"
          @click="handleResetFilters"
        >
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar
      title="权限列表"
      :columns="columns"
      :tableRef="tableRef"
      @refresh="handleSearch"
      @fullscreen="tableRef.value?.setAdaptive?.()"
    >
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(Plus)"
          @click="openCreate"
        >
          新建权限
        </el-button>
      </template>

      <template #default="barScope">
        <template v-if="loading && tableData.length === 0">
          <el-skeleton :rows="10" animated />
        </template>
        <template v-else>
          <PureTable
            ref="tableRef"
            v-loading="loading"
            :columns="barScope?.dynamicColumns ?? columns"
            :data="tableData"
            :size="barScope?.size ?? 'default'"
            :pagination="pagination"
            @page-current-change="handlePageChange"
            @page-size-change="handleSizeChange"
          >
            <template #type="{ row }">
              {{
                permissionTypes.find(option => option.value === row.type)
                  ?.label ?? row.type
              }}
            </template>
            <template #clientType="{ row }">
              {{
                clientTypes.find(option => option.value === row.client_type)
                  ?.label ?? row.client_type
              }}
            </template>
            <template #status="{ row }">
              <el-tag
                :type="row.status === 1 ? 'success' : 'danger'"
                size="small"
                effect="plain"
              >
                {{ row.status === 1 ? "已启用" : "已禁用" }}
              </el-tag>
            </template>
            <template #operation="{ row, size }">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :icon="useRenderIcon(EditPen)"
                :size="size"
                @click="openEdit(row)"
              >
                编辑
              </el-button>
              <el-popconfirm
                :title="`确认删除权限「${row.name}」吗？`"
                @confirm="handleDelete(row)"
              >
                <template #reference>
                  <el-button
                    class="reset-margin"
                    link
                    type="danger"
                    :icon="useRenderIcon(Delete)"
                    :size="size"
                  >
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </PureTable>
        </template>
      </template>
    </PureTableBar>

    <el-dialog
      v-model="permissionDialogVisible"
      :title="permissionDialogTitle"
      width="480px"
      destroy-on-close
      :close-on-click-modal="false"
    >
      <el-form
        ref="permissionFormRef"
        :model="permissionForm"
        :rules="rules"
        label-width="100px"
        status-icon
      >
        <el-form-item label="权限名称" prop="name">
          <el-input
            v-model="permissionForm.name"
            placeholder="请输入权限名称"
            maxlength="80"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input
            v-model="permissionForm.code"
            placeholder="例如：system.config.edit"
            maxlength="100"
            show-word-limit
            :disabled="permissionForm.id !== 0"
          />
        </el-form-item>
        <el-form-item label="权限类型" prop="type">
          <el-select v-model="permissionForm.type" placeholder="请选择权限类型">
            <el-option
              v-for="item in permissionTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属端" prop="client_type">
          <el-select
            v-model="permissionForm.client_type"
            placeholder="请选择端类型"
          >
            <el-option
              v-for="item in clientTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="permissionForm.status"
            :inactive-value="0"
            :active-value="1"
          />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input
            v-model="permissionForm.description"
            type="textarea"
            :rows="3"
            maxlength="200"
            show-word-limit
            placeholder="用于说明权限使用场景，便于后续维护"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="permissionDialogVisible = false">取消</el-button>
          <el-button type="primary" :loading="saving" @click="submitPermission">
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.permissions-page {
  padding: 12px 12px 20px;
}

.search-form :deep(.el-form-item) {
  margin-bottom: 12px;
}

.search-form :deep(.el-form-item__label) {
  padding-right: 6px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
</style>
